package cn.com.controller.manageController;


import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Parts;
import cn.com.db.service.PartsService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Auth(UserAuth.ADMIN)
@RequestMapping("/back/parts")
@RestController
public class PartsManageController {

    @Autowired
    PartsService partsService;

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String createParts(Parts parts){
        DataResult dataResult=new DataResult();
        if (StringUtils.isNotBlank(parts.getParent())&&
                !partsService.isExist(parts.getParent())){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("父级不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        parts.setId(UidUtil.createUUID());
        parts.setCreated_at(TimeUtil.getNowTimestamp());
        parts.setUpdated_at(TimeUtil.getNowTimestamp());
        partsService.createParts(parts);
        dataResult.setData(parts);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("添加成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String getParts(@RequestParam("parent")String parent){
        DataResult dataResult=new DataResult();
        dataResult.setTotal(partsService.countParts(parent));
        dataResult.setData(partsService.getParts(parent));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public String viewParts(@PathVariable("id")String id){
        DataResult dataResult=new DataResult();
        if (!partsService.isExist(id)){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("配件不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        dataResult.setData(partsService.getPartsById(id));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("获取成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Parts parts){
        DataResult dataResult=new DataResult();
        if (!partsService.isExist(parts.getId())){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("配件不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        if (StringUtils.isNotBlank(parts.getParent())&&
                !partsService.isExist(parts.getParent())){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("父级不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        partsService.updateParts(parts);
        dataResult.setData(partsService.getPartsById(parts.getId()));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("编辑成功");
        return JsonUtil.toJsonString(dataResult);
    }



}
